package com.ruoyi.common.utils;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;

import java.io.IOException;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Slf4j
public class SMS {

    private static String Url = "http://106.ihuyi.cn/webservice/sms.php?method=Submit";

    public static void seng(String code, String mobile) {
        HttpClient client = new HttpClient();
        PostMethod method = new PostMethod(Url);
        client.getParams().setContentCharset("GBK");
        method.setRequestHeader("ContentType", "application/x-www-form-urlencoded;charset=GBK");
        String content = "您的验证码是：" + code + "。请不要把验证码泄露给其他人。";
        NameValuePair[] data = {//提交短信
                new NameValuePair("account", "C47272297"), //查看用户名是登录用户中心->验证码短信->产品总览->APIID
                new NameValuePair("password", "26bb7bb3f3ff6e967903f0a40255db6d"),  //查看密码请登录用户中心->验证码短信->产品总览->APIKEY
                //new NameValuePair("password", util.StringUtil.MD5Encode("密码")),
                new NameValuePair("mobile", mobile),
                new NameValuePair("content", content),
                new NameValuePair("format", "json"),
        };
        method.setRequestBody(data);
        try {
            client.executeMethod(method);
            String SubmitResult = method.getResponseBodyAsString();
            log.info("向 " + mobile + " 发送通知响应：" + SubmitResult);
        } catch (HttpException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    private static final String[] PREFIXES = {
            "130", "131", "132", "133", "134", "135", "136", "137", "138", "139", "145", "147", "149",
            "150", "151", "152", "153", "155", "156", "157", "158", "159", "166", "170", "171", "172",
            "173", "175", "176", "177", "178", "179", "180", "181", "182", "183", "184", "185", "186",
            "187", "188", "189", "190", "191", "192", "193", "194", "195", "196", "197", "198", "199"
    };

    public static String generatePhoneNumber() {
        Random random = new Random();
        int prefixIndex = random.nextInt(PREFIXES.length);
        String prefix = PREFIXES[prefixIndex];
        String number = String.format("%08d", random.nextInt(99999999)); // 生成8位随机数
        while (!isMobile(prefix + number)) {
            generatePhoneNumber();
        }
        return prefix + number; // 拼接成完整的手机号码
    }

    public static boolean isMobile(String phone) {
        String regex = "^((13[0-9])|(14[5,7,9])|(15([0-3]|[5-9]))|(166)|(17[0,1,2,3,5,6,7,8,9])|(18[0-9])|(19[0,1,2,3,5,6,7,8,9]))\\d{8}$";
        if (phone.length() != 11) {
            return false;
        } else {
            Pattern p = Pattern.compile(regex);
            Matcher m = p.matcher(phone);
            return m.matches();
        }
    }
}
